﻿using System.IO;

namespace Common
{
    public class IoCommon
    {       /// <summary>
        /// Xoa file
        /// </summary>
        /// <param name="strPath"></param>
        /// <returns></returns>
        public static bool DeleteFile(string strPath)
        {
            var file = new FileInfo(strPath);
            if (file.Exists)
            {
                file.Delete();
                return true;
            }
            return false;
        }

        /// <summary>
        /// Kiem tra xem file co ton tai hay khong
        /// </summary>
        /// <param name="strPath"></param>
        /// <returns></returns>
        public static bool IsExist(string strPath)
        {
            var file = new FileInfo(strPath);
            return file.Exists;
        }

        /// <summary>
        /// Xoa thu muc
        /// </summary>
        /// <param name="strPath"></param>
        /// <returns></returns>
        public static bool DeleteDirectory(string strPath)
        {
            if (Directory.Exists(strPath))
            {
                Directory.Delete(strPath, true);
                return true;
            }
            return false;
        }

        /// <summary>
        /// Read file and return a text
        /// </summary>
        /// <param name="filePath"></param>
        /// <param name="fileName"></param>
        /// <returns></returns>
        public static string ReadFile(string filePath, string fileName)
        {
            // Check existed directory, create if it's not
            if (!Directory.Exists(filePath))
            {
                Directory.CreateDirectory(filePath);
            }

            // Check existed file, create if it's not then goto result step
            filePath = Path.Combine(filePath, fileName);
            if (!File.Exists(filePath))
            {
                FileStream f = File.Create(filePath);
                f.Close();
                f.Dispose();
                return string.Empty;
            }

            // Open file and read all text
            TextReader file = new StreamReader(filePath);
            string strLine = file.ReadToEnd();
            file.Close();
            file.Dispose();

            return strLine.Replace("\r\n", "").Replace("\t", "").Trim();
        }

        /// <summary>
        /// Write text to file
        /// </summary>
        /// <param name="text"></param>
        /// <param name="filePath"></param>
        /// <param name="fileName"></param>
        /// <returns></returns>
        public static bool WriteFile(string text, string filePath, string fileName)
        {
            // Check existed directory, create if it's not
            if (!Directory.Exists(filePath))
            {
                Directory.CreateDirectory(filePath);
            }

            // Check existed file, create if it's not then goto result step
            filePath = Path.Combine(filePath, fileName);
            if (!File.Exists(filePath))
            {
                FileStream f = File.Create(filePath);
                f.Close();
                f.Dispose();
                return false;
            }

            // Open file and write text
            TextWriter file = new StreamWriter(filePath);
            file.Write(text);
            file.Close();
            file.Dispose();

            return true;
        }
    }
}
